package com.demo.service;

import com.demo.pojo.Article;
import com.demo.pojo.ArticleExample;
import com.demo.service.lucene.ArticleLucene;
import com.demo.service.lucene.MyIKAnalyzer;
import com.sun.xml.internal.bind.api.impl.NameConverter;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.lucene.IKAnalyzer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service("LuceneService")
public class LuceneService {
    @Resource
    private ArticleService as;
    Directory directory = new RAMDirectory();
    Analyzer analyzer = new MyIKAnalyzer();
////    StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
    @PostConstruct
    public void init() throws IOException {
        createArticleIndex();
    }
    public void createArticleIndex() throws IOException {
        List<Article> articleList = as.getArticlesWithFullInfo();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
        for(Article article:articleList){
            Document document = new Document();
            document.add(new StringField("id",article.getId().toString(), Field.Store.YES));
            document.add(new TextField("title",article.getTitle(),Field.Store.YES));
            document.add(new TextField("content",article.getArticleContent().getContent(),Field.Store.YES));
            indexWriter.addDocument(document);
        }
        indexWriter.close();
    }
    public List<Article> search(String content) throws Exception {
        List<Article> articleList = new ArrayList<>();
        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                new String[]{"title","content"},analyzer
        );
//        QueryParser queryParser = new QueryParser("content",analyzer);
        Query query =queryParser.parse(content);
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(reader);
        TopDocs results = indexSearcher.search(query,10);
        System.out.println("共"+results.totalHits+"个结果");
        ScoreDoc[] scoreDocs = results.scoreDocs;
        if(scoreDocs !=null){
            for(ScoreDoc scoreDoc : scoreDocs){
                int docId = scoreDoc.doc;
                Document document = indexSearcher.doc(docId);
                System.out.println(document.get("id"));
                articleList.add(as.getArticleWithFullInfo(Integer.parseInt(document.get("id"))));
            }
        }
        reader.close();
        return articleList;
    }
}
